<?php
class FluentExt{
	private function fill_place_holder(&$arr, $with = false){
		$i = 0;
		foreach($arr as &$a){
			if($a == '?') $a = $with == false ? $this->c : $with;
			$i++;
		}
		return $i;
	}
	
	protected function fill_ret($arr, $with = false){
		foreach($arr as $a){
			if($a == '?') 	$ret[] = $with == false ? $this->c : $with;
			else			$ret[] = $a;
		}
		return $ret;
	}
	
	function get_place_holder($arr){
		$num = 0;
		foreach($arr as &$a){
			if($a == '?') $a = '&$a->c';
			if(is_array($a) || is_object($a)){
				$this->t["temp".$num] = $a;
				$a 					  = "\$a->t['temp$num']";
				$num++;
			}
		}
		return $arr;
	}
	
	function apply(){
		$params 	= func_get_args();
		$cont_pos	= $this->fill_place_holder($params);
		$el 		= array_shift($params);
		if(strlen($el) == 1){
			$ret 	= $el;
			$f 		= array_shift($params);
		}else{
			$f 		= $el;
			$ret	= false;
		}
		switch($ret){
			case 'r':
				return call_user_func_array($f, $params);
				break;
			default:
				$this->c = call_user_func_array($f, $params);
				return $this;
				break;
		}
	}
	
	function smarty(&$smarty){ $this->smarty =& $smarty; return $this; }
	
	function assign($key){ $this->smarty->assign($key, $this->c); return $this; }
	
	function fetch($template){ return new Str($this->smarty->fetch($template)); }
}
?>